package com.wan.activity

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.ActionBarDrawerToggle
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.google.gson.JsonObject
import com.wan.R
import com.wan.adapter.MainPageAdapter
import com.wan.base.WanApp
import com.wan.fragment.*
import com.wan.uitls.ApiManager
import com.wan.uitls.CommonUtils
import com.wan.uitls.RetrofitHelper
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : AppCompatActivity() {
    private val mThreadName = this.javaClass.name

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()

        initPageAndBottomView()

        initNavigationView()
    }

    private fun initNavigationView() {
        var headerView = main_navigation_nv.getHeaderView(0)
        var imageView = headerView.findViewById<ImageView>(R.id.main_avatar_iv)
        var userNameTv = headerView.findViewById<TextView>(R.id.main_user_name_tv)
        var options = RequestOptions()
        options.circleCrop()
        options.placeholder(R.mipmap.ic_launcher)

        Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .apply(options)
                .into(imageView)
        imageView.setOnClickListener {
            startActivity(Intent(this, LoginActivity::class.java))
        }
        main_navigation_nv.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.main_collection -> {
                    if (CommonUtils.getLoginState(this)) {
                        startActivity(Intent(this, CollectionActivity::class.java))
                    } else {
                        startActivity(Intent(this, LoginActivity::class.java))
                    }
                }
                R.id.main_setting -> {
                    CommonUtils.toastWord(this, "请期待")
                }
                R.id.main_logout -> {
                    if (CommonUtils.getLoginState(this)) {
                        logout()
                    }
                }
                R.id.main_about -> {
                    startActivity(Intent(this, AboutActivity::class.java))
                }
            }

            true
        }


    }

    /**
     * 退出登录
     *
     */
    private fun logout() {
        var apiManager = ApiManager(this)
        apiManager.getLogoutData().enqueue(object : Callback<JsonObject> {
            override fun onFailure(call: Call<JsonObject>, t: Throwable) {
                Log.d(mThreadName, " onResponse 退出失败:${call.execute().body()} ")
                CommonUtils.toastWord(this@MainActivity, "退出失败")

            }

            override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
                Log.d(mThreadName, " onResponse 退出登录:${response.body()} ")
                CommonUtils.saveLoginState(this@MainActivity, false)
                CommonUtils.toastWord(this@MainActivity, "已退出登录")
            }
        })
    }

    private fun initPageAndBottomView() {
        var fragments: ArrayList<Fragment> = ArrayList()
        fragments.add(HomeFragment())
        fragments.add(ProjectFragment())
        fragments.add(SystemFragment())
        fragments.add(NavigationFragment())
        var pageAdapter = MainPageAdapter(supportFragmentManager, fragments)
        main_content_vp.adapter = pageAdapter
        main_content_vp.offscreenPageLimit = fragments.size - 1

        CommonUtils.disableShiftMode(main_footer_bv)
        main_footer_bv.setOnNavigationItemSelectedListener {
            println("menuItem => $it")
            when (it.itemId) {
                R.id.bottom_navigation_home_item -> {
                    main_content_vp.currentItem = 0
                }
                R.id.bottom_navigation_project_item -> {
                    main_content_vp.currentItem = 1

                }
                R.id.bottom_navigation_system_item -> {

                    main_content_vp.currentItem = 2
                }
                R.id.bottom_navigation_navigation_item -> {
                    main_content_vp.currentItem = 3

                }
            }
            main_head_tl.title = it.title
            true
        }

    }

    private fun initView() {
        setSupportActionBar(main_head_tl)
        var actionBarDrawerToggle = ActionBarDrawerToggle(this, main_drawer_dl, main_head_tl, R.string.app_name, R.string.app_name)
        actionBarDrawerToggle.syncState()
        //添加与抽屉的互动效果
        main_drawer_dl.addDrawerListener(actionBarDrawerToggle)
    }

    override fun onDestroy() {
        super.onDestroy()
        WanApp.threadPool.shutdown()
        RetrofitHelper.get().cancelAllCall()
        var manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        manager.killBackgroundProcesses(packageName)
        System.exit(0)
    }
}

